<template>
  <div>
    <el-form ref="gysForm" size="small" :model="entity" :rules="rules" label-width="120px">
      <el-form-item label="供应商名称" prop="name">
        <el-input v-model="entity.name" placeholder="供应商名称" />
      </el-form-item>
      <el-form-item label="供应商简介" prop="descption">
        <el-input type="textarea" resize="none" :autosize="{ minRows: 3, maxRows: 3 }" v-model="entity.descption" placeholder="供应商简介" />
      </el-form-item>
      <el-form-item label="私有化报价" prop="jiage">
        <el-input v-model="entity.jiage" placeholder="私有化报价" />
      </el-form-item>
      <el-form-item label="公有云报价" prop="jiagenet">
        <el-input v-model="entity.jiagenet" placeholder="公有云报价" />
      </el-form-item>
      <el-form-item label="服务明细" prop="fuwudesc">
        <div style="border: 1px solid #ccc;">
          <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode"/>
          <Editor style="height: 500px; overflow-y: hidden;"  @onCreated="onCreated"
            v-model="entity.fuwudesc" :defaultConfig="editorConfig" :mode="mode"
          />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from '@/utils/request'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

export default {
  components: { Editor, Toolbar },
  props:{
    entityId : {type : Number, default: 0},
    xmId : {type : Number, default: 0}
  },
  data() {
    return {
      photoId : 0,
      entity: {
        id : this.entityId,
        name: '',
        descption: '',
        jiage: '',
        jiagenet: '',
        fuwudesc:'',
      },
      rules: {
        name: [
          { required: true, message: '请输入供应商名称', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        descption: [
          { required: true, message: '请输入供应商简介', trigger: 'blur' },
        ],
      },
      editor: null,
      toolbarConfig: { },
      editorConfig: { placeholder: '请输入内容...' },
      mode: 'default', // or 'simple'
    }
  },
  mounted() {
    this.loadEntity();
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  },
  methods: {
    loadEntity(){
      if(this.entityId > 0){
        request({ url: '/gys/getgys', data: {id : this.entityId} }).then( res =>{
          this.entity = res.data
        })
      }
    },
    onSubmit(){
      this.entity.xmid = this.xmId;
      this.$refs['gysForm'].validate((valid) => {
        if(valid){
          request({
            url: '/gys/addeditgys',
            data: this.entity
          }).then( res =>{
            if(res.code === "200"){
              this.$message.success(res.msg)
              this.$emit('update:visible', false);  //关闭弹窗
              this.entityId > 0 ? this.$EventBus.$emit("gysedit", {}):this.$EventBus.$emit("gysadd", {})
            }else{
              this.$message.error(res.msg);
            }
          })
        }else{
          return false;
        }
      })
    },
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
  }
}
</script>

<style scoped>

</style>
